/**
 * Created With IntelliJ IDEA
 * Description:leetcode:162. 寻找峰值
 * <a href="https://leetcode.cn/problems/find-peak-element/">...</a>
 * User: DELL
 * Data: 2023-03-08
 * Time: 22:33
 */
public class Solution {
    public int findPeakElement(int[] nums) {
        return find(nums,0,nums.length-1);
    }
    private int find (int[] num, int left, int right) {
        if (left == right) {
            return left;
        }
        int mid = (left + right) / 2;
        //这里处理mid==0的情况,不需要处理mid==num.length的情况,因为当left==right时会直接结束递归
        if (mid == 0) {
            if (num[0] > num[1]) {
                return 0;
            }
            return 1;
        }
        if (num[mid-1] > num[mid]) {
            return find(num,left,mid-1);
        } else if (num[mid+1] > num[mid]) {
            return find(num,mid+1,right);
        } else {
            return mid;
        }
    }
}